﻿using System;
using System.Windows;
using System.Windows.Documents;
using CommonFoundation.DataSource;
using CommonFoundation.DataModel;
using Microsoft.Office.Interop.Excel;
using CommonFoundation;
using System.IO;

namespace WHULive
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();

        }

        private void ImportLessonButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                LogInWindow logInWindow = new LogInWindow();

                logInWindow.ShowDialog();
            }
            catch (Exception ex)
            {
                Log.WriteLog(ex.Message);
            }

            if (BindingSource.GetInstance().Lessons.Count == 0)
            {
                MessageBox.Show("导入失败，请重试!");
            }
            else
            {
                this.UpdateLessonsTable();
            }
        }

        private void UpdateLessonsTable()
        {
            this.LessonsTable.RowGroups.Clear();

            this.LessonsTable.RowGroups.Add(new TableRowGroup());


            TableRow columnsNameRow = new TableRow();

            for (int i = 0; i < 8; i++)
            {
                this.LessonsTable.Columns.Add(new TableColumn());
                this.LessonsTable.Columns[i].Name = this.GetColumnName(i);

                TableCell tmpTableCell = new TableCell(new Paragraph(new Bold(new Run(this.LessonsTable.Columns[i].Name))));

                tmpTableCell.TextAlignment = TextAlignment.Center;

                columnsNameRow.Cells.Add(tmpTableCell);
            }

            this.LessonsTable.RowGroups[0].Rows.Add(columnsNameRow);

            for (int row = 0; row < 14; row++)
            {
                TableRow currentTableRow = new TableRow();

                for (int col = 0; col < 8; col++)
                {
                    currentTableRow.Cells.Add(this.GetTableCell(row, col));
                }

                this.LessonsTable.RowGroups[0].Rows.Add(currentTableRow);
            }
        }

        private TableCell GetTableCell(int row, int col)
        {

            TableCell tableCell = new TableCell (new Paragraph(new  Run(this.GetCellContent(row, col))));

            return tableCell;
        }

        private string GetCellContent(int row, int col)
        {
            if (col == 0)
            {
                return String.Format("第{0}节课", (row + 1).ToString());
            }

            foreach (Lesson lesson in BindingSource.GetInstance().Lessons)
            {
                foreach (Schedule schedule in lesson.Schedules)
                {
                    if (schedule.IsInClass(col % 7, row + 1))
                    {
                        return lesson.ToString() + schedule.Location;
                    }
                }
            }

            return String.Empty;
        }

        private string GetColumnName(int index)
        {
            if (index == 0)
            {
                return String.Empty;
            }

            return Consts.WeekDayNames[index % Consts.WeekDaysNumber];
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (BindingSource.GetInstance().Lessons.Count == 0)
            {
                MessageBox.Show("请先导入数据！");

                return;
            }

            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();

            Workbook workbook = excelApp.Workbooks.Add(System.Type.Missing);

            Worksheet worksheet = workbook.Worksheets[1];

            for (int col = 0; col < 8; col++)
            {
                worksheet.Cells[1, col + 1] = this.GetColumnName(col);
            }

            for (int row = 0; row < 14; row++)
            {
                for (int col = 0; col < 8; col++)
                {
                    worksheet.Cells[row + 2, col + 1] = this.GetCellContent(row, col);
                }

            }

            try
            {
                workbook.SaveAs(FilePath.GetInstance().SaveFilePath);
            }
            catch (IOException ex)
            {
                MessageBox.Show("导出失败，请检查文件路径。");
                Log.WriteLog(ex.Message);
            }

            workbook.Close();
            excelApp.Quit();
        }

        private void SetButton_Click(object sender, RoutedEventArgs e)
        {
            SetWindow setWindow = new SetWindow();

            setWindow.ShowDialog(); 
        }

        private void AddLessonButton_Click(object sender, RoutedEventArgs e)
        {
            AddScheduleWindow window = new AddScheduleWindow();

            window.ShowDialog();

            this.UpdateLessonsTable();
        }

        private void SyncToCalendarButton_Click(object sender, RoutedEventArgs e)
        {
            GoogleAccount googleAccount = new GoogleAccount();

            googleAccount.ShowDialog();

            bool flag = true;

            foreach (Lesson lesson in BindingSource.GetInstance().Lessons)
            {
                MyCalendar calendar = new MyCalendar(BindingSource.GetInstance().GoogleID, BindingSource.GetInstance().GooglePassword);

                if (!calendar.UpdateLesson(lesson))
                {
                    flag = false;
                }
            }

            if (!flag)
            {
                MessageBox.Show("同步失败");
            }
            else
            {
                MessageBox.Show("同步完成");
            }
        }

    }
}
